<?php

namespace app\user\controller;

use think\Controller;
use think\Request;
use think\Db;
use app\user\model\Userdata;

class User extends Controller
{
   
    /**
     * ajax注册验证
     * @return JSON [description]
     */
    public function register_v(){
        

        $username = input('post.username');
        $password = md5(input('post.password'));
        $phone = input('post.phone');
        $email = input('post.email');
        $new = new Userdata();
        $new -> username = $username;
        $new -> password = $password;
        $new -> phone = $phone;
        $new -> email = $email;
        $a = $new -> save();
        if ($a) {
            $_SESSION['user'] = $username;
            $_SESSION['uid'] = $new -> id;
            echo json_encode(['status' => '500','user' => $username,'uid' => $new->id]);
        }else{
            echo json_encode(['status' => '1']);
        }

        
    }

    public function register_name()
    {
        if(Userdata::get(['username' => input('post.username')])){
            echo json_encode(['status'=>403]);
        }else{
            echo json_encode(['status'=>200]);
        }

    }
    /**
     * ajax注册验证
     * @return JSON [description]
     */
    public function login_v()
    {
        $result = Userdata::get(['username'=>input('post.username'),'password'=>md5(input('post.password'))]);
        if (!captcha_check(input('post.captcha'))) {
            echo json_encode(['captcha'=>'failed']);
        }elseif(!$result){
            echo json_encode(['search'=>'noUser']);
            
        }else{
            echo json_encode(['status' => '1']);

            // 自动登录
            if (input('post.checked')=='checked') {
                cookie('user',input('post.username'),3600*6);
                cookie('uid',$result->id,3600*6);
            }else{
                // 设置cookie
                cookie('user',input('post.username'),3600*24);
                cookie('uid',$result->id,3600*24);
            }
            
        }
    }

    /**
     * 找密码
     * @return [type] [description]
     */
    public function findPwd()
    {

        return view('find_pwd');
    }

    /**
     * 发送邮件方法
     * @return [type] [description]
     */
    public function sendMail()
    {
        /**
             * tp5邮件
             * @param
             * @author staitc7 <static7@qq.com>
             * @return mixed
             */
        $content = '密码重置链接已经发送到你的邮箱，请进入您的邮箱完成密码修改';
        if (!captcha_check(input('post.captcha'))) {
            header("Location: http://tp.com/user/user/findPwd");
            $content = '验证码有误';
            $this->assign('content',$content);
            return view('send_mail');
        }
        $this->assign('content',$content);
        $toemail=input('post.email');
        $name='NIKo';
        $subject='盘古租车用户密码找回';
        // 存入redis
        $redis = new \think\cache\driver\Redis();
        $redis->set(md5(input('post.email')),input('post.email'));
        $link = 'http://106.14.8.76/user/user_admin/pwd/?email='.md5(input('post.email'));
        $content='您好！请点击以下链接完成密码修改<a href="'.$link.'">'.'请点击此链接'.'</a>';
        send_mail($toemail,$name,$subject,$content);
        
        return view('send_mail');
    }
    /**
     * 密码修改页面
     * @return [type] [description]
     */
    public function modPwd()
    {
        return view('mod_pwd');
    }
    /**
     * 密码确认
     * @return [type] [description]
     */
    public function pwd_v()
    {
        return view();
    }

    
}
